import json
import os
import time
from datetime import datetime

import requests

from Base.HttpBase import HttpBase
from Base import Log, GetYamlFile
from Base.ReadExcel import Read_Excel

log = Log.Log()
base_url = 'https://ly.laihui.cn'  # 铜川线上环境
# 需要手动修改所需环境id 及name
tenant_id = 2185
tenant_name = '铜川声威建材有限责任公司'

login_api = base_url + '/upms-provider/tenant/user/login/mobile'
login_data = {
    'mobile': '18368093710',
    'password': 'Aa123456'
}
headers = {
    'Content-Type': 'application/json'
}

is_list = []
# resp = requests.post(login_api, json=login_data, headers=headers)
# log.info(resp.headers)
# token = resp.headers['access_token']
# Refresh_token = resp.headers['Refresh_token']
# log.info(token)
# 基础数据

# 基础请求头
base_headers = {
    'type': 'management',
    'tenant_id': str(tenant_id),
    'access_token': '35dd88d5-cc21-4ede-8056-8c5d4e143bbf',
    'Refresh_token': 'e41866d4-3988-4f7f-b1c6-45dba67db5a6',
    # 'Version': 'middleground-2.0.5.6-SNAPSHOT'
}


def phone_add():
    yml_list = GetYamlFile.getYamlFile()
    carPath = yml_list["customerPhonePath"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, carPath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("sheet")
    customer_not_list = []
    for i in range(0, num):
        customer_id = readBook.read_Excel("sheet", i, 0)
        customer_name = readBook.read_Excel("sheet", i, 1)
        customer_phone = readBook.read_Excel("sheet", i, 2)
        print("customer_id:{},customer_name:{},customer_phone:{}".format(customer_id, customer_name, customer_phone))
        # 通过customer_id查询客户
        customer_page_url = base_url + '/tenant-lms-basic-provide/lms/basic/customer/page?current=1&size=10&type=1&auditStatus=1&code=' + customer_id
        customer_examine_url = base_url + '/tenant-lms-basic-provide/lms/basic/customer/examine'
        customer_edit_url = base_url + '/tenant-lms-basic-provide/lms/basic/customer/edit'
        result = requests.get(customer_page_url, headers=base_headers)
        # print(base_headers)
        # print(str(result.content, 'utf-8'))
        customer_list = json.loads(str(result.content, 'utf-8')).get("data").get("records")
        # print(customer_list)
        json_headers = base_headers
        json_headers["Content-Type"] = 'application/json'

        if len(customer_list) != 0:
            id = customer_list[0].get("id")
            # print("客户详情：{}".format(customer_list[0]))
            customer_examine_data = {"id": id, "auditStatus": 0}
            requests.put(customer_examine_url, customer_examine_data, headers=json_headers)
            customer_edit_data = {"code": customer_list[0].get("code"), "tenantId": "2185",
                                  "tenantName": "铜川声威建材有限责任公司", "name": customer_list[0].get("name"),
                                  "customerCategoryId": customer_list[0].get("customerCategoryId"),
                                  "type": customer_list[0].get("type"),
                                  "businessLicense": customer_list[0].get("businessLicense"),
                                  "shortNameCn": customer_list[0].get("shortNameCn"),
                                  "shortName": customer_list[0].get("shortName"),
                                  "contact": customer_list[0].get("contact"),
                                  "otherId": customer_list[0].get("otherId"),
                                  "address": customer_list[0].get("address"), "mobile": customer_phone,
                                  "phone": None, "isFranchisee": customer_list[0].get("isFranchisee"),
                                  "customerType": customer_list[0].get("customerType"),
                                  "remark": customer_list[0].get("remark"),
                                  "id": customer_list[0].get("id")}
            # print(customer_edit_data)
            result = requests.put(customer_edit_url, json=customer_edit_data, headers=json_headers)
            print(str(result.content, 'utf-8'))
            customer_examine_data = {"id": id, "auditStatus": 1}
            requests.put(customer_examine_url, customer_examine_data, headers=json_headers)
        else:
            print("不存在该用户：{}".format(customer_name))
            customer_not_list.append(customer_name)
            print("不存在该用户列表：{}".format(customer_not_list))


if __name__ == '__main__':
    # phone_add()
    # 获取当前时间
    now = datetime.now()
    timestamp = int(now.timestamp() * 1000)
    print(timestamp)
